﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Blokus.Controls;

namespace Blokus.Scenes
{
    public class SceneDesignerMenu : Scene
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private SpriteFont menuFont;
        private readonly int menuDesignerPiece;
        private readonly int menuDesignerBoard;
        private readonly int menuDesignerCreate;
        private readonly int menuDesignerEdit;
        private readonly int menuExit;

        private List<string> menuItems = new List<string>();
        private int currentMenuIndex = 0;
        private KeyboardState keyPrevState;
        private GamePadState gpPrevState;
        private float aspectRatio;
        private RenderTarget2D backgroundRenderTarget;

        TextBox btnPiece;
        TextBox btnBoard;
        TextBox btnCreate;
        TextBox btnBack;
        Texture2D rectn;

        public SceneDesignerMenu()
        {
            menuDesignerPiece = menuItems.Count;
            menuItems.Add("Design Piece");
            menuDesignerBoard = menuItems.Count;
            menuItems.Add("Designer Board");
            menuDesignerCreate = menuItems.Count;
            menuItems.Add("Create Games");
            menuDesignerEdit = menuItems.Count;
            menuItems.Add("Edit Games");
            menuExit = menuItems.Count;
            menuItems.Add("Quit");
        }

        public override void LoadContent(ContentManager content, GraphicsDeviceManager graphics)
        {
            keyPrevState = Keyboard.GetState();
            this.graphics = graphics;
            //load font
            menuFont = content.Load<SpriteFont>("gamefont");
            rectn = content.Load<Texture2D>("Textures/rectangle");
            backgroundRenderTarget = new RenderTarget2D(graphics.GraphicsDevice, 1280, 800);
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

            //texBG = content.Load<Texture2D>("Textures\\background");
            aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;

            btnPiece = new TextBox("Design Piece", false, new Rectangle(150, 600, 180, 50));
            btnPiece.padding = 75;
            btnPiece.backgroundTexture = rectn;
            btnPiece.spriteBatch = spriteBatch;
            btnPiece.GameFont = menuFont;
            btnPiece.colorOnMouseOver = true;
            btnPiece.isFocusable = false;

            btnBoard = new TextBox("Design Board", false, new Rectangle(550, 600, 180, 50));
            btnBoard.padding = 75;
            btnBoard.backgroundTexture = rectn;
            btnBoard.spriteBatch = spriteBatch;
            btnBoard.GameFont = menuFont;
            btnBoard.colorOnMouseOver = true;
            btnBoard.isFocusable = false;

            btnCreate = new TextBox("Create Games", false, new Rectangle(950, 600, 180, 50));
            btnCreate.padding = 75;
            btnCreate.backgroundTexture = rectn;
            btnCreate.spriteBatch = spriteBatch;
            btnCreate.GameFont = menuFont;
            btnCreate.colorOnMouseOver = true;
            btnCreate.isFocusable = false;

            btnBack = new TextBox("Back", false, new Rectangle(50, 735, (int)menuFont.MeasureString("Back").X, 50));
            btnBack.backgroundTexture = rectn;
            btnBack.spriteBatch = spriteBatch;
            btnBack.GameFont = menuFont;
            btnBack.colorOnMouseOver = true;
            btnBack.isFocusable = false;
        }

        public int milliDemoCounter = 0;
        public override Blokus.GameState Update(GameTime gameTime)
        {
            if (btnPiece.Update(gameTime, Mouse.GetState()))
            {
                return Blokus.GameState.DESIGNER_PIECE;
            }
            if (btnBoard.Update(gameTime, Mouse.GetState()))
            {
                return Blokus.GameState.DESIGNER_BOARD;
            }
            if (btnCreate.Update(gameTime, Mouse.GetState()))
            {
                return Blokus.GameState.DESIGNER_CREATION;
            }
            if(btnBack.Update(gameTime,Mouse.GetState()))
            {
                return Blokus.GameState.MENU;
            }
            //number of menu items minus one
            //int menuMax = 5;
            ////get keys
            GamePadState currGPState = GamePad.GetState(PlayerIndex.One);
            bool kEsc = Keyboard.GetState().IsKeyDown(Keys.Escape) && !keyPrevState.IsKeyDown(Keys.Escape);
            if (kEsc)
            {
                return Blokus.GameState.MENU;
            }
            //    || currGPState.IsButtonDown(Buttons.DPadUp) && !gpPrevState.IsButtonDown(Buttons.DPadUp);
            //bool kDown = Keyboard.GetState().IsKeyDown(Keys.Down) && !keyPrevState.IsKeyDown(Keys.Down)
            //    || currGPState.IsButtonDown(Buttons.DPadDown) && !gpPrevState.IsButtonDown(Buttons.DPadDown);
            //bool kLeft = Keyboard.GetState().IsKeyDown(Keys.Left) && !keyPrevState.IsKeyDown(Keys.Left)
            //    || currGPState.IsButtonDown(Buttons.DPadLeft) && !gpPrevState.IsButtonDown(Buttons.DPadLeft);
            //bool kRight = Keyboard.GetState().IsKeyDown(Keys.Right) && !keyPrevState.IsKeyDown(Keys.Right)
            //    || currGPState.IsButtonDown(Buttons.DPadRight) && !gpPrevState.IsButtonDown(Buttons.DPadRight);
            //bool kEnter = Keyboard.GetState().IsKeyDown(Keys.Enter) && !keyPrevState.IsKeyDown(Keys.Enter)
            //    || currGPState.IsButtonDown(Buttons.A) && !gpPrevState.IsButtonDown(Buttons.A);
            //milliDemoCounter += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
            //if (kUp || kDown || kLeft || kRight || kEnter)
            //    milliDemoCounter = 0;


            ////check for menu movement

            //if (kUp)
            //{
            //    currentMenuIndex--;
            //    if (currentMenuIndex < 0)
            //        currentMenuIndex = menuMax;
            //}
            //if (kDown)
            //{
            //    currentMenuIndex++;
            //    if (currentMenuIndex > menuMax)
            //        currentMenuIndex = 0;
            //}

            ////check for menu item selected
            //if (kEnter)
            //{
            //    if (currentMenuIndex == menuDesignerPiece)
            //    {
            //        return Blokus.GameState.DESIGNER_PIECE;
            //    }
            //    else if (currentMenuIndex == menuDesignerBoard)
            //    {
            //        return Blokus.GameState.DESIGNER_BOARD;
            //    }
            //    else if (currentMenuIndex == menuDesignerCreate)
            //    {
            //        return Blokus.GameState.DESIGNER_CREATION;
            //    }
            //    else if (currentMenuIndex == menuDesignerEdit)
            //    {
            //        return Blokus.GameState.DESIGNER_EDIT;
            //    }
            //    else if (currentMenuIndex == menuExit)
            //    {
            //        return Blokus.GameState.MENU;
            //    }
            //}

            //reset previous keyboard state
            keyPrevState = Keyboard.GetState();
            gpPrevState = GamePad.GetState(PlayerIndex.One);

            return Blokus.GameState.DESIGNER_MENU;
        }

        public override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(colorMiddle);
            spriteBatch.Begin();
            //menu items
            //spriteBatch.Draw(texBG, new Rectangle(0, 0, 1280, 800), Color.White);
            spriteBatch.DrawString(menuFont, "BLOKUS++", new Vector2(550, 175), colorDark);
            //for (int ct = 0; ct < menuItems.Count; ct++)
            //{
            //    Color clr = colorLightest;
            //    Random random = new Random();
            //    if (currentMenuIndex == ct)
            //        clr = colorDark;
            //    string menuStr = menuItems[ct];
            //    spriteBatch.DrawString(menuFont, menuStr, new Vector2(100, 200 + ct * 50), clr);
            //}
            btnPiece.Draw();
            btnBoard.Draw();
            btnCreate.Draw();
            btnBack.Draw();
            spriteBatch.End();
        }

        public override void ResetPrevKeys()
        {
            keyPrevState = Keyboard.GetState();
            gpPrevState = GamePad.GetState(PlayerIndex.One);
            SceneHighscores.newHighscore = -1;
            milliDemoCounter = 0;
        }
    }
}
